package edu.spec.yjx.controller;

import edu.spec.common.model.AjaxReturn;
import edu.spec.yjx.model.News;
import edu.spec.yjx.service.iface.INewsService;
import org.madgecko.basic.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;


/**
 * Author       : MadFrog
 * Date         : 2018/11/10 12:48
 * Description  :
 */
@Controller
public class NewsController {

    @Autowired
    private INewsService newsService;

    @RequestMapping(value="/admin/news/add/page")
    public String go2AddPage() {
        return "admin/news/add";
    }

    @ResponseBody
    @RequestMapping(value="/admin/news/add")
    public AjaxReturn add(News news) {
        news.setCreateDate(new Date());
        newsService.add(news);
        return new AjaxReturn(true, "添加成功");
    }

    @ResponseBody
    @RequestMapping(value="/admin/news/delete/{id}")
    public AjaxReturn delete(@PathVariable Integer id) {
        newsService.delete(id);
        return new AjaxReturn(true, "添加成功");
    }

    @RequestMapping(value="/news/view/{id}")
    public ModelAndView view(@PathVariable Integer id) {
        News news = newsService.load(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("news", news);
        modelAndView.setViewName("common/newsView");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value="/admin/news/findByCon")
    public AjaxReturn find(String keyword) {
        Pager<News> pager =  newsService.findByCon(keyword);
        return new AjaxReturn(true, pager.getDatas(), pager.getTotal());
    }
}
